package com.wdf.helper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtils {
	
	   private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	   
	   private static long minute = 60*1000;
	   private static long hour = minute *60;
	   private static long day = hour *24;
	   private static long year = 365*day;
       
	   public static String timePast(String time){
		   if(time == null)
			   return "";
		   if( time.indexOf('.') >0){
			   time = time.substring(0, time.indexOf('.'));
		   }
		 try {
				Date date =   formatter.parse(time);
			    long past = 	 System.currentTimeMillis() - date.getTime();
			 if(past > year){
				 return " 发布于 "+past/year +"年前";
			 }else if(past>day){
				 return " 发布于 "+past/day +"天前";
			 }else if(past>hour){
				 return " 发布于 "+past/hour +"小时前";
			 }else if(past>minute){
				 return " 发布于 "+past/minute +"分钟前";
			 }else {
				 return " 发布于 "+past/1000 +"秒前";
			 }
			} catch (Exception e) {
				 return time;
			}
		  
		  
	   }
}
